package kr.hkit.powerjava.project_cafe;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

import java.awt.Font;
import java.awt.GridLayout;

public class Dialog_Output02 extends JDialog {
	private JTable table;
	private JPanel panel_2;
	private static ProductDAO dao;
	private Product temp;
	private JPanel panel_1;
	private Comparator<Product> comp;
	private int rank;
	private Product temp2;
	private double sumSupply = 0;
	private double vat;
	private double supply;
	private double marginPrice;
	private ArrayList<Product> prod;
	private JLabel lblNewLabel;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			Dialog_Output02 dialog = new Dialog_Output02(dao);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public Dialog_Output02(ProductDAO dao) {
		super();
		this.dao = dao;
		initialize();
	}

	private void initialize() {
		setTitle("출력2 - 마진액순위");
		//setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		setSize(800, 300);

		panel_1 = new JPanel();
		getContentPane().add(panel_1, BorderLayout.NORTH);
		panel_1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		JLabel lblLabel01 = new JLabel("마 진 액 순 위");
		lblLabel01.setFont(new Font("맑은 고딕", Font.BOLD, 18));
		lblLabel01.setHorizontalAlignment(SwingConstants.CENTER);
		panel_1.add(lblLabel01);

		panel_2 = new JPanel();
		getContentPane().add(panel_2, BorderLayout.CENTER);
		panel_2.setLayout(new BorderLayout(0, 0));
		
		loadData2();

		JScrollPane scrollPane = new JScrollPane(table);
		panel_2.add(scrollPane, BorderLayout.CENTER);
		getContentPane().add(panel_2);

		scrollPane.setViewportView(table);
		
		/*table = new JTable();
		panel_2.add(table, BorderLayout.CENTER);
		getContentPane().add(panel_2);*/
		
		tableCellAlginAndWidth_cafe2();
		table.setAutoCreateRowSorter(true);
		
		
		
		/*addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				super.windowClosing(e);
				dispose();
			}
		});*/
		
	}
	
	
	public void loadData2() { //jtable에 넣기 
		
		prod = new ArrayList<>();
		int suppplySum = 0, vatSum = 0, sellSum = 0, marSum = 0;
		
		try {
			prod = dao.getProdList2();
		} catch (SQLException e) {
			System.out.println("쿼리오류");
			e.printStackTrace();
		}

		String[] columns = { "제품코드", "제품명", "제품단가", "판매수량", "공급가액",
				"부가세액", "판매금액", "마진율", "마진액" };
		String[][] data2 = new String[prod.size()+1][columns.length]; // 행수는?
		DecimalFormat df = new DecimalFormat("#,###.#"); //포멧
		DecimalFormat df1 = new DecimalFormat("#,###"); //
		
		
		//계산 여기서 
		for (int i = 0; i < prod.size(); i++) {
			temp = prod.get(i);
			
			vat = (temp.getSellingPrice()/11); // 부가세액
			supply = (temp.getSellingPrice() - vat); // 공급가액
			marginPrice = (supply * temp.getProdMargin()); // 마진액
			
			data2[i] = new String[] {/*temp.getRank()+""*/ temp.getProdId(),
					temp.getProdName(), temp.getProdPrice() + "",
					temp.getProdSales() + "", df.format(supply), df.format(vat),
					df1.format(temp.getSellingPrice()), temp.getProdMargin() + "",
					df.format(marginPrice)};
			
			suppplySum += supply;
			vatSum += vat;
			sellSum += temp.getSellingPrice();
			marSum += marginPrice;
		}
		
		
		data2[prod.size()][0] = "합계";
		data2[prod.size()][4] = df1.format(suppplySum);
		data2[prod.size()][5] = df1.format(vatSum);
		data2[prod.size()][6] = df1.format(sellSum);
		data2[prod.size()][8] = df1.format(marSum);
		
		DefaultTableModel model2 = new DefaultTableModel(data2, columns);
		table = new JTable(model2);
		

	}
	
	
	/*
	public void ranking(){ // ??? 
		Comparator<Product> comp = new Comparator<Product>() {

			@Override
			public int compare(Product o1, Product o2) {
				return o1.getSellingPrice() - o2.getSellingPrice();
			}
		};
	}
	
	*/
	
	public void tableCellAlginAndWidth_cafe2(){
		
		DefaultTableCellRenderer dtcrCenter = new DefaultTableCellRenderer();
		dtcrCenter.setHorizontalAlignment(SwingConstants.CENTER);
		
		DefaultTableCellRenderer dtcrRight = new DefaultTableCellRenderer();
		dtcrRight.setHorizontalAlignment(SwingConstants.RIGHT);
		
		TableColumnModel cModel = table.getColumnModel();
		
		//cModel.getColumn(0).setCellRenderer(dtcrCenter);
		cModel.getColumn(0).setCellRenderer(dtcrCenter);
		cModel.getColumn(1).setCellRenderer(dtcrCenter);
		cModel.getColumn(2).setCellRenderer(dtcrRight);
		cModel.getColumn(3).setCellRenderer(dtcrRight);
		cModel.getColumn(4).setCellRenderer(dtcrRight);
		cModel.getColumn(5).setCellRenderer(dtcrRight);
		cModel.getColumn(6).setCellRenderer(dtcrRight);
		cModel.getColumn(7).setPreferredWidth(40);
		cModel.getColumn(7).setCellRenderer(dtcrRight);
		cModel.getColumn(8).setPreferredWidth(100);
		cModel.getColumn(8).setCellRenderer(dtcrRight);
		
	}
	
}
	
	/*
	public void loadData2()  { // 백터써보기? 실패..
		
		Vector<String> userColumn = new Vector<String>();
		Vector<String> userRow;
		
		userColumn.addElement("순위");
		userColumn.addElement("제품코드");
		userColumn.addElement("제품명");
		userColumn.addElement("제품단가");
		userColumn.addElement("판매수량");
		//userColumn.addElement("공급가액");
		//userColumn.addElement("부가세액");
		//userColumn.addElement("판매금액");
		//userColumn.addElement("마진율");
		//userColumn.addElement("마진액");
		
		
		ArrayList<Product> data2 = null;
		try {
			 data2 = dao.getProdList2();
		} catch (SQLException e) {
			System.out.println("쿼리오류!!");
			e.printStackTrace();
		}
		
		//int rank = 1; // 순위 수정할 것
		//double vat = prod.getSellingPrice() /11; // 부가세액
		//double supply = (prod.getSellingPrice() - vat); // 공급가액
		//double marginPrice = (supply * prod.getProdMargin()); // 마진액

		userRow = new Vector<String>();
		userRow.addElement("1");
		//userRow.addElement(data2.get(1)+"");
		//userRow.addElement(data2.get(2)+"");
		//userRow.addElement(data2.get(3)+"");
		//userRow.addElement(data2.get(4)+""); //판매수량
		//userRow.addElement(); //공급가액
		
		//String[] columns = { "순위", "제품코드", "제품명", "제품단가", "판매수량", "공급가액",
				//"부가세액", "판매금액", "마진율", "마진액" };
		
		DefaultTableModel model2 = new DefaultTableModel(userColumn, 0);
		table = new JTable(model2);
		
		//String[][] data3 = new String[data2.length][columns.length];
		
		

		for (int i = 0; i < 10; i++) {
			data2[i] = new String[] { rank + "", prod.getProdId(),
					prod.getProdName(), prod.getProdPrice() + "",
					prod.getProdSales() + "", supply + "", vat + "",
					prod.getSellingPrice() + "", prod.getProdMargin() + "",
					marginPrice + "" };
		};

		
	}
*/
	
	
	


